package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

import static Driver.DriverSingleton.waitForWindow;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class ConsultationPage extends BasePage{

    /*
    1.开始会话
     */

    //从homepage跳转到咨询界面
    public void toConsultationPage() {
        waitForWindow(4000);
        driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/ul/ul/li/div")).click();

        waitForWindow(4000);
        assertThat(driver.getTitle(), is("会话列表"));

    }

    //从该界面跳转回首页
    public void toHomePage() {
        waitForWindow(8000);
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(1)")).click();
        waitForWindow(2000);
        assertThat(driver.getTitle(), is("首页"));
    }

    //获取当前会话状态
    public String getConversationState(){
        String info = driver.findElement(By.xpath("//*[@id=\"conversation\"]/div[1]/div[1]/div[2]/p")).getText();
        return info;
    }


    /*
    2.发送消息
     */

    //输入文字消息
    public ConsultationPage inputTextMessage(String message){

        //在对话框中输入文字
        driver.findElement(By.cssSelector(".text-input")).click();
        driver.findElement(By.cssSelector(".text-input")).sendKeys(message);
        return this;

    }

    //使用Enter键发送
    public ConsultationPage sendByEnter(){
        driver.findElement(By.cssSelector(".text-input")).sendKeys(Keys.ENTER);
        return this;
    }

    //使用发送键发送
    public ConsultationPage sendByButton(){
        driver.findElement(By.xpath("//*[@id=\"message-send-box-wrapper\"]/div[4]/div/button")).click();
        return this;
    }

    //获取发送的文字消息
    public String getTextMessage(){

        waitForWindow(2000);
        String mes = "";
        List<WebElement> elements =  driver.findElements(By.cssSelector(".message-content"));

        //获取发送的最后一条消息
        mes = elements.get(elements.size()-1).getText();
        return mes;

    }

    //点击输入语音按钮并发送
    public ConsultationPage sendVoice(){
        waitForWindow(2000);
        driver.findElement(By.xpath("//*[@id=\"message-send-box-wrapper\"]/div[1]/div")).click();
        waitForWindow(6000);
        driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div[2]/div[1]/div[2]/div/div/div[2]/div/div[1]/div/span/div/div/button[2]")).click();
        return this;
    }

    //点击选择一个表情输入
    public ConsultationPage inputExpression(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".conversation-item-container")).click();
        driver.findElement(By.cssSelector(".icon-xiaolian")).click();
        driver.findElement(By.cssSelector(".emoji:nth-child(1) > img")).click();
        return this;
    }

    //返回最后发送表情的src
    public String getExpression(){

        waitForWindow(2000);
        String mes = "";
        WebElement element =  driver.findElement(By.xpath("//*[@id=\"conversation\"]/div[1]/div[2]/div/div/div[1]/div[1]/div[3]/div[1]/div[2]/div[1]/div[2]/div/div/div[2]/img"));

        //获取发送的最后一条消息
        mes = element.getAttribute("src");
        System.out.println(mes);
        return mes;

    }

    //发送图片
    public ConsultationPage sendImage(){
        driver.findElement(By.cssSelector(".icon-tupian1")).click();
        driver.findElement(By.id("imagePicker")).sendKeys("C:\\Users\\18019\\Pictures\\moon.jpg");
        //waitForWindow(8000);
        //driver.findElement(By.cssSelector(".focusing .image-element")).click();

        return this;

    }

    //检测图片
    public String getImage(){
        waitForWindow(8000);
        String s =driver.findElement(By.xpath("//*[@id=\"conversation\"]/div[1]/div[2]/div/div/div[1]/div[1]/div[4]/div[1]/div[2]/div[1]/div[2]/div/div/div[2]/img")).getAttribute("class");
        return s;

    }


    /*
    3.结束会话
     */

    //咨询师点击结束会话
    public ConsultationPage clickFinish(){
        driver.findElement(By.cssSelector(".btns > div:nth-child(2)")).click();
        return this;
    }

    //咨询师点击弹窗中的确认
    public ConsultationPage confirm(){
        driver.findElement(By.xpath("//button[contains(.,'确认')]")).click();
        return this;
    }

    //咨询师点击弹窗中的取消
    public ConsultationPage cancel(){
        driver.findElement(By.xpath("/html/body/div[2]/div/div[3]/button[1]")).click();
        return this;
    }

    public ConsultationPage cancel1(){
        driver.findElement(By.cssSelector("body > div.el-message-box__wrapper > div > div.el-message-box__btns > button:nth-child(1)")).click();
        return this;
    }

    //咨询师点击弹窗中的关闭
    public ConsultationPage close(){
        driver.findElement(By.xpath("//button[contains(.,'关闭')]")).click();
        return this;
    }

    //返回webdriver
    public WebDriver getDriver(){
        return this.driver;
    }


    /*
    4.求助督导
     */

    //点击求助督导
    public ConsultationPage request(){
        driver.findElement(By.cssSelector(".btns > div:nth-child(1)")).click();
        return this;
    }

    //点击发起求助
    public ConsultationPage sendRequest(){
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".custmBtn")).click();
        return this;
    }

    //获取当前活跃会话状态
    public String getActiveWindowState(){
        WebElement element = driver.findElement(By.cssSelector(".btnActive"));
        String state = element.getText();
        return state;
    }

    //点击右侧督导会话
    public ConsultationPage clickRequestWindow(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".current-conversation > .content:nth-child(2)")).click();
        return this;
    }

    //点击左侧访客会话
    public ConsultationPage clickVisitorWindow(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".content:nth-child(1)")).click();
        return this;
    }

    //点击求助督导弹窗中的取消
    public ConsultationPage cancelRequest(){
        waitForWindow(2000);
        driver.findElement(By.xpath("//*[@id=\"conversation\"]/div[3]/div/div/div[3]/div/button")).click();
        return this;
    }

    //点击结束求助
    public ConsultationPage clickFinishRequest(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".finished > .finishIcon")).click();
        return this;
    }

    /*
    5.向督导发送消息
     */

    //输入文字消息
    public ConsultationPage inputRequestTextMessage(String message){

        //在对话框中输入文字
        driver.findElement(By.cssSelector(".footer:nth-child(3) > #message-send-box-wrapper .text-input")).click();
        driver.findElement(By.cssSelector(".footer:nth-child(3) > #message-send-box-wrapper .text-input")).sendKeys(message);
        return this;

    }

    //使用Enter键发送
    public ConsultationPage sendRequestByEnter(){
        driver.findElement(By.cssSelector(".footer:nth-child(3) > #message-send-box-wrapper .text-input")).sendKeys(Keys.ENTER);
        return this;
    }

    //使用发送键发送
    public ConsultationPage sendRequestByButton(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".footer:nth-child(3) > #message-send-box-wrapper > .bottom .el-button")).click();
        return this;
    }

    //获取发送的文字消息
    public String getRequestTextMessage(){

        waitForWindow(2000);
        String mes = "";
        List<WebElement> elements =  driver.findElements(By.cssSelector(".message-content"));

        //获取发送的最后一条消息
        mes = elements.get(elements.size()-1).getText();
        return mes;

    }

    //点击输入语音按钮并发送
    public ConsultationPage sendRequestVoice(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".el-icon-microphone")).click();
        return this;
    }

    //点击发送的语音消息
    public ConsultationPage getRequestVoiceMessage(){

        waitForWindow(2000);
        driver.findElements(By.xpath("/html/body/div[1]/div/div[2]/div[2]/div[1]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div/"));

        return this;

    }


    //点击选择一个表情输入
    public ConsultationPage inputRequestExpression(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".footer:nth-child(3) > #message-send-box-wrapper .el-popover__reference-wrapper > .iconfont")).click();
        waitForWindow(2000);
        driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[1]/img")).click();

        return this;
    }

    //返回最后发送表情的src
    public String getRequestExpression(){

        waitForWindow(2000);
        String mes = "";
        WebElement element =  driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div[2]/div[1]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div/div/div[2]/img"));

        //获取发送的最后一条消息
        mes = element.getAttribute("src");
        System.out.println(mes);
        return mes;

    }

    //发送图片
    public ConsultationPage sendRequestImage(){
        driver.findElement(By.cssSelector(".footer:nth-child(3) .messageMenus > .iconfont")).click();
        driver.findElement(By.cssSelector(".footer:nth-child(3) #imagePicker")).sendKeys("C:\\Users\\18019\\Pictures\\moon.jpg");
        //waitForWindow(8000);
        //driver.findElement(By.cssSelector(".focusing .image-element")).click();

        return this;

    }

    //检测图片
    public String getRequestImage(){
        waitForWindow(8000);
        String s =driver.findElement(By.xpath("//*[@id=\"conversation\"]/div[1]/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div/div/div[2]/div/div")).getAttribute("class");
        return s;
    }



    /*
    6.查看访客历史咨询记录
     */

    //咨询师查看访客历史咨询记录
    public String checkHistoryRecord(){
        //点击查看详情
        driver.findElement(By.cssSelector(".customShareMessage")).click();
        driver.findElement(By.cssSelector(".free > span")).click();

        //获取查询的记录
        waitForWindow(2000);
        String info = driver.findElement(By.xpath("//*[@id=\"conversation\"]/div[1]/div[2]/div/div/div[1]/div[1]/div[7]/div[3]/div/div/div[1]/div")).getText();

        //关闭查看详情页面
        driver.findElement(By.cssSelector(".seleHistoruModal > .el-dialog__footer:nth-child(3) .el-button")).click();
        driver.findElement(By.cssSelector(".seleModal > .el-dialog__footer:nth-child(3) span")).click();

        return info;
    }

    /*
    7.获取当前咨询咨询内容
     */

    public String getAllCurrentConversationRecord(){
        String info = "";
        List<WebElement> elements =  driver.findElements(By.cssSelector(".message-content"));
        for(WebElement element : elements){
            info+=element.getText();
            info += "\n";
        }
        System.out.println(info);
        return info;
    }


}
